package com.sxu.greedy.day2;


import java.util.ArrayList;

/**
 * 最长递增子序列
 * 动态规划 + 贪心 + 二分
 */
public class Test5 {
    public int lengthOfLIS(int[] nums) {
        ArrayList<Integer> ret = new ArrayList<>();
        int n = nums.length;
        ret.add(nums[0]);

        for(int i = 1; i < n; i++) {
            if(nums[i] > ret.get(ret.size() - 1)) {
                ret.add(nums[i]);
            } else {
                // 二分插入
                int left = 0, right = ret.size() - 1;
                while(left < right) {
                    int mid = (left + right) / 2;
                    if(ret.get(mid) < nums[i]) {
                        left = mid + 1;
                    } else {
                        right = mid;
                    }
                }
                ret.set(left, nums[i]);
            }
        }
        return ret.size();
    }
}
